/**
 * Created by kitabi on 2017/6/20.
 */
/**
 * Created by kitabi on 2017/6/16.
 */
var classify={

};

var $btnStaffConfirm= $("#btnStaffConfirm");
var $staffDialog= $("#staffDialog");
/**
 * 获取用户数据
 */
classify.queryList= function(){


    $("#mainTable").bootstrapTable({

        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,                              //是否显示行间隔色
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        url:"/offertype/gryTypeList",
        // height: $(window).height() - 110,
        // width:$(window).width(),
      //  showColumns:true,
        pagination:true,
        // queryParams : queryParams,
        minimumCountColumns:2,
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
        showExport: true,
        exportDataType: 'all',


        paginationPreText:"<",
        paginationNextText:">",
        queryParams: function(params) {

            var userSearch= $("#userSearch").val();
            return {
                cPage: (params.offset/params.limit)+1,
                pSize: params.limit,

                typeName:userSearch,

            };
        },
        columns: [
            {
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            },{
                field: 'indexs',
                title: '排序'
            }, {
                field: 'type_name',
                title: '分类名称'
            }, {

                field: 'status',
                title: '操作',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var id= row.id;

                    var e='<button type="button" onclick="showDetail('+index+',\'update\')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">编辑</button>';
                    var d='<button type="button" onclick="del('+id+')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">删除</button>';
                    return  e+d;
                }
            }]

    });

}

function updateStatus(id ,type){
    var param={
        id: id,
        u_status: type
    }
    Ajax.ajax({
        url: "/user/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function del(id){
    var param = {
        id:id
    }
    Ajax.ajax({
        url: "/offertype/delete",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                // alert(res.data);
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.resultMsg);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function addType() {
    var brandName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(brandName == ""){
        alert("请输入分类名称");
        return;
    }
    if(indexs ==""){
        alert("请输入序号");
        return;
    }

    var param = {
        brandName:brandName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/add",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function clearDialog() {
    $("#Sname").val("");
    $("#indexs").attr("data-id","");
    $("#indexs").val("");
    $("#userSearch").val();
    $staffDialog.hide();
}

function showDetail(index,type){

    if(type == "add"){

        $btnStaffConfirm.off('click');
        $btnStaffConfirm.on('click',addType);
    }else if(type == "update"){
        var data= $("#mainTable").bootstrapTable("getData")[index];//获取当前列的数据


        $("#indexs").attr('data-id',data.id);
        $("#indexs").val(data.indexs);
        $("#Sname").val(data.type_name);
        $btnStaffConfirm.off('click');
        $btnStaffConfirm.on('click',updateType);
    }

    $staffDialog.show();

}
function updateType() {
    var id = $("#indexs").attr('data-id');
    var offerTypeName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(offerTypeName == ""){
        alert("请输入名称");
        return;
    }
    var param = {
        id:id,
        offerTypeName:offerTypeName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/update",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

$(function(){
    //关闭窗口
    $('#staffDialogClose').on('click',function(){

        $staffDialog.hide();
    });

    classify.queryList();



    $("#userSearchBtn").click(function(){
        $("#mainTable").bootstrapTable('refresh');
    });
});